import data from "../../../../core/cache_data";
import { SubjectComponent } from "../../../../core/subject";
import RadioGroup2 from "../../../../prefabs/radio_group2";
import { WorldData } from "../../world_data";

const { ccclass, property } = cc._decorator;

@ccclass
export default class BoxLoadWorld extends SubjectComponent {
    @property(RadioGroup2)
    rd: RadioGroup2 = null;
    @property(cc.Prefab)
    lbPrefab: cc.Prefab = null;

    async onClickLoad() {
        if (!this.rd.cursor.target) {
            return;
        }
        this.emit("load", this.rd.curInd);
        this.node.destroy();
    }

    protected onLoad(): void {
        let worldList: WorldData[] = data.storage.get<WorldData[]>("editor-world-list", []);
        worldList.forEach(worldData => {
            let item = cc.instantiate(this.lbPrefab);
            item.name = worldData.key;
            cc.find("lb", item).getComponent(cc.Label).string = worldData.key;
            this.rd.addItem(item);
        });

    }
}
